/**
 * RtmpRecv.h
 * 使用 rtmpdump API 接收 RTMP 流
 */

#ifndef _RTMPRECV_H_
#define _RTMPRECV_H_

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>

#include "../librtmp/rtmp.h"
#include "../librtmp/log.h"

#include "Rtp.h"
#include "RtpInstance.h"
#include "InetAddress.h"
#include "SocketOps.h"

using namespace std;

class RtmpRecv {
private:
    bool bLiveStream;   // isLive stream ?
    string url;         // rtmp stream url
    string fileName;    // pathName for the flv file
    FILE *fp;           // file pointer
    char *buf;          // buf for reading rtmp stream
    int bufSize;        // buffer size
    long countBufSize;  // receive rtmp stream bufSize

    RTMP *rtmp;         // rtmp struct

    RtpPacket* rtpPacket;   // RTP 
    RtpInstance* rtpInstance;
    string destIP;          // multicast IP
    uint16_t destPort;      // multicast Port
    int FPS;

public:
    RtmpRecv(const string& s_url, const string file_name, 
        int sockfd, uint16_t port, const string& destIP, uint16_t destPort,
        int fps);

    ~RtmpRecv();

    /**
     * 初始化 rtmp, 并测试 RTMP_SetupURL, RTMP_Connect, RTMP_ConnectStream
     * 成功返回 1，失败返回 0
     */
    int init(); 

    /**
     * 接收 RTMP 流并处理
     * 1. 存储成 flv 文件
     * 2. 封装到 rtpPacket.payload 中
     * 3. 发送给客户端
     */
    void recv_send();
};

#endif